package org.example;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.*;

public class SMTPClient {
    static String ser_ip="smtp.qq.com";
    static String subject="^-^";

    public static void main(String[] args) { 
        String sender = "1246625854@qq.com";
        String receiver = "ysylggxy@163.com";
        String password = "vlxcqhljgcaggahi";
        String ID = Base64.ToBase64(sender.getBytes());
        String pass = Base64.ToBase64(password.getBytes());
        try { 
            Socket socket = new Socket(ser_ip, 25);
            InputStream input = socket.getInputStream();
            OutputStream output = socket.getOutputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "gbk"), true);
            System.out.println(reader.readLine());

            writer.println("HELO gxy");;
            System.out.println(reader.readLine());
            System.out.println(reader.readLine());
            System.out.println(reader.readLine());
            //AUTH LOGIN 
            writer.println("auth login");
            System.out.println(reader.readLine());
            writer.println(ID);
            System.out.println(reader.readLine());
            writer.println(pass);
            System.out.println(reader.readLine());

            writer.println("MAIL FROM:<" + sender +">");
            System.out.println("MAIL FROM:<" + sender +">");
            System.out.println(reader.readLine());
            writer.println("RCPT TO:<" + receiver +">");
            System.out.println("RCPT TO:<" + receiver +">");
            System.out.println(reader.readLine()); 
           
            //Set data 
            writer.println("data");
            System.out.println(reader.readLine());
            writer.println("subject:" + subject);
            writer.println("from:" + sender);
            writer.println("to:" + receiver);
            writer.println();
            writer.println("这是测试");
            writer.println(".");
            writer.println("");
            System.out.println(reader.readLine());

            writer.println("quit");
            writer.flush();
            System.out.println(reader.readLine());
            input.close();
            output.close();
            socket.close();
            } catch (Exception e) {
                 e.printStackTrace();
           }
        System.out.println("发送成功");
        } 
  }